/*******************************************************************************
 * Copyright (c) 2011 Olexandr V. Shaposhnikov.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Olexandr V. Shaposhnikov - initial API and implementation and/or initial documentation
 *******************************************************************************/
package com.funkyroach.msvctch.msvc2008;

import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.envvar.IBuildEnvironmentVariable;
import org.eclipse.cdt.managedbuilder.envvar.IConfigurationEnvironmentVariableSupplier;
import org.eclipse.cdt.managedbuilder.envvar.IEnvironmentVariableProvider;
import org.eclipse.cdt.managedbuilder.internal.envvar.BuildEnvVar;

public class MSVC2008ConfigurationEnvironmentVariableSupplier implements
		IConfigurationEnvironmentVariableSupplier {

	static final String PATH_VARNAME = "PATH"; //$NON-NLS-1$
	static final String INCLUDE_VARNAME = "INCLUDE"; //$NON-NLS-1$
	static final String LIB_VARNAME = "LIB"; //$NON-NLS-1$

	static final String DELIMITER_WIN = ";"; //$NON-NLS-1$
	static final String PROPERTY_DELIMITER = "path.separator"; //$NON-NLS-1$
	static final String PROPERTY_OSNAME = "os.name"; //$NON-NLS-1$
	private static final String VCINSTALLDIR = "VCINSTALLDIR";
	private static final String WindowsSdkDir = "WindowsSdkDir";
	private static final String DirectXSDK = "DirectXSDK";

	public MSVC2008ConfigurationEnvironmentVariableSupplier() {
	}

	public IBuildEnvironmentVariable getVariable(String variableName,
			IConfiguration configuration, IEnvironmentVariableProvider provider) {

		if (!System.getProperty(PROPERTY_OSNAME).toLowerCase().startsWith(
				"windows ")) //$NON-NLS-1$ 
			return null;

		if (variableName == null)
			return null;

		if (PATH_VARNAME.equalsIgnoreCase(variableName)) {
			StringBuffer var = new StringBuffer();
			final String msvc = MSVC2008PathResolver.getMsvcHome();

			var.append(msvc).append("\\bin;");
			var.append(msvc).append("\\vcpackages;");
			var.append(msvc).append("\\..\\Common7\\Tools;");
			var.append(msvc).append("\\..\\Common7\\IDE;");

			var.append("%PATH%");
			return new BuildEnvVar(PATH_VARNAME, var.toString().replace('/',
					'\\'), IBuildEnvironmentVariable.ENVVAR_PREPEND, System
					.getProperty(PROPERTY_DELIMITER, DELIMITER_WIN)); //$NON-NLS-1$ //$NON-NLS-2$
		} else if (VCINSTALLDIR.equalsIgnoreCase(variableName)) {
			return new BuildEnvVar(VCINSTALLDIR, MSVC2008PathResolver
					.getMsvcHome(), IBuildEnvironmentVariable.ENVVAR_APPEND,
					System.getProperty(PROPERTY_DELIMITER, DELIMITER_WIN)); //$NON-NLS-1$ //$NON-NLS-2$;
		} else if (WindowsSdkDir.equalsIgnoreCase(variableName)) {
			return new BuildEnvVar(WindowsSdkDir, MSVC2008PathResolver
					.getMsSDKHome(), IBuildEnvironmentVariable.ENVVAR_APPEND,
					System.getProperty(PROPERTY_DELIMITER, DELIMITER_WIN)); //$NON-NLS-1$ //$NON-NLS-2$;
		} else if (DirectXSDK.equalsIgnoreCase(variableName)) {
			return new BuildEnvVar(DirectXSDK, MSVC2008PathResolver
					.getDXSDKHome(), IBuildEnvironmentVariable.ENVVAR_APPEND,
					System.getProperty(PROPERTY_DELIMITER, DELIMITER_WIN)); //$NON-NLS-1$ //$NON-NLS-2$;
		}
		// if (variableName.indexOf(';') != -1) {
		// String vars[] = variableName.split(";");
		// }

		return null;
	}

	public IBuildEnvironmentVariable[] getVariables(
			IConfiguration configuration, IEnvironmentVariableProvider provider) {

		IBuildEnvironmentVariable tmp[] = new IBuildEnvironmentVariable[4];
		tmp[0] = getVariable(PATH_VARNAME, configuration, provider);
		tmp[1] = getVariable(VCINSTALLDIR, configuration, provider);
		tmp[2] = getVariable(WindowsSdkDir, configuration, provider);
		tmp[3] = getVariable(DirectXSDK, configuration, provider);
		return tmp;
	}
}
